package com.zp.self.多数据源工具;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 拦截器控制，从参数、请求头中获取变量，设置此次线程的数据源
* @author  ZengPeng
* @date  2021/4/15
**/
@Slf4j
public class TenantInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String db = request.getParameter("db");
        if(StringUtils.isBlank(db)){
//            response.sendRedirect("/login.html");//直接返回登录页面，不处理此请求
            return true;
        }else{
            DynamicDataSourceContextHolder.setContextKey(db);
            return true;
        }
    }
}

